package common.controller;


import implementor.MaterialMasterImplementor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JOptionPane;

import model.Material;

import common.view.MaterialAdderView;


import dao.MaterialMasterDao;
import dto.MaterialMaster;
import dto.MaterialSpecification;


import utility.ViewUtilities;

/*
 * Finalized on 29 OCT 2013.
 * Tested by @author Komal.
 * 
 */
public class MaterialAdderController implements ActionListener,ItemListener{
	private MaterialAdderView view;
	private Material[] materials;
	private Material model;
	
	
	public MaterialAdderController() {
		// TODO Auto-generated constructor stub
	}

	public MaterialAdderController(MaterialAdderView view,
			Material model) throws Exception {
		super();
		this.view = view;
		this.model = model;
		this.filMaterialNameCombo();
	
		view.getCbMaterialName().addItemListener(this);
		
	}

	
	
	
	

	public void filMaterialNameCombo() throws Exception
	{
		MaterialMasterDao mDao=new MaterialMasterImplementor();
		MaterialMaster[] master=null;
		
		try {
			master = mDao.retreiveAll();      // Retrieve all material from db.
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] materialNames=new String[master.length+1]; // Initalize combo box array
		materialNames[0]="--Select Material Name--";
		materials=new Material[master.length];            // Initialize actual material arrary size must
		                                                  // as retreived master.
		// material array should be synchronized with master
		for(int i=1;i<=master.length;i++)
		{
			materialNames[i]=master[i-1].getmName();			// FILL THE MATERIAL NAME COMBOBOX
			materials[i-1]= new Material(master[i-1].getmCode());	// FILL THE MATERAIL ARRAY FROM ALL THE MATERIALS IN THE DB
			
		}
		utility.ViewUtilities.set_comboBox(view.getCbMaterialName(),materialNames);
		String[]specnames = new String[]{"--Select Specification----"};
		ViewUtilities.set_comboBox(view.getCbMaterialSpecification(), specnames);
		
		
	
	
	}
	
	
	/**
	 * Will get called when selection in material Name comboBox changes
	 */
	public void chngeSpecOnSelectedMaterial()
	{
		int index=view.getCbMaterialName().getSelectedIndex();
		String[] specnames;
		// only change specification when selection is valid
		if(index!=0)
		{
		   // get specification corresponding to selected material
			MaterialSpecification[] spec=materials[index-1].getSpec();
		    specnames=new String[spec.length+1];  // combobox length must be greter by 0ne from specification
		   int i=0;
		  specnames[0]="--Select Specification----";
		  
		  for(MaterialSpecification ms:spec)
			specnames[i+1]=spec[i].getmSpecs();
		ViewUtilities.set_comboBox(view.getCbMaterialSpecification(), specnames);
		}
		else
		{
		  specnames = new String[]{"--Select Specification----"};
		  ViewUtilities.set_comboBox(view.getCbMaterialSpecification(), specnames);
		}
		
	}
	
// This method will be called when add buton is clicked. It returns a material object
	
	public void changeModel()
	{
		model = this.materials[view.getCbMaterialName().getSelectedIndex()-1];
		model.setCurrentSpecs(model.getSpec()[view.getCbMaterialSpecification().getSelectedIndex()-1]);
		
	}
	
	// This method returns model object which is responsible for passing current material.
	public Material getModel() {
		return model;
	}
	public MaterialAdderView getView() {
		return view;
	}
	
/****************************************************DYNAMIC PART************************************************/	
	
	
	

	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		
	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getStateChange() == ItemEvent.SELECTED) {
		   chngeSpecOnSelectedMaterial();
		}
	}
}
